package com.kodcu.web.menubar;

import com.kodcu.web.book.BookMainPage;
import com.kodcu.web.home.HomePage;
import com.kodcu.web.iletisim.IletisimMainPage;
import com.kodcu.web.referanslar.ReferanslarMainPage;
import com.kodcu.web.training.TrainingPage;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

public class Menu extends Panel {

    private static final long serialVersionUID = 1L;

    public Menu(String id, PageParameters params) {
        super(id);

        Label title = new Label("MenuBaslik", "");
        add(title);
        String selected = params.getString("0");

        BookmarkablePageLink<Page> anaSayfa = new BookmarkablePageLink<Page>(
                "AnaSayfa", HomePage.class);
        putSelected(selected, anaSayfa, "Anasayfa", title);
        add(anaSayfa);

        BookmarkablePageLink<Page> egitimler = new BookmarkablePageLink<Page>(
                "Egitimler", TrainingPage.class);
        putSelected(selected, egitimler, "E\u011fitimler", title);
        add(egitimler);

        PageParameters paramsForKitap = new PageParameters();
        paramsForKitap.add("0", "java-kitap");
        BookmarkablePageLink<Page> kitap = new BookmarkablePageLink<Page>(
                "java-kitap", BookMainPage.class, paramsForKitap);
        putSelected(selected, kitap, "Kitap", title);
        add(kitap);

        BookmarkablePageLink<Page> referanslar = new BookmarkablePageLink<Page>(
                "Referanslar", ReferanslarMainPage.class);
        putSelected(selected, referanslar, "Referanslar", title);
        add(referanslar);


        BookmarkablePageLink<Page> iletisim = new BookmarkablePageLink<Page>(
                "Iletisim", IletisimMainPage.class);
        putSelected(selected, iletisim, "Iletisim", title);
        add(iletisim);


    }


    /**
     * Replace selected css accoring to user selection to top menu
     *
     * @param selected Selected Page name
     * @param pageLink Link name
     */
    private void putSelected(String selected, BookmarkablePageLink<Page> pageLink
            , String title, Label menuTitle) {

        if (selected == null) {
            // selected value is null, no need to run
            return;
        }




        String linkName = pageLink.getId();
        int linkNumber = 1;
        if (selected.equalsIgnoreCase(linkName)) {
            pageLink.add(new SimpleAttributeModifier("class", "selected"));
            menuTitle.setDefaultModel(new Model<String>(title));
        }  else {
            pageLink.add(new SimpleAttributeModifier("class", ""));
        }




    }

}
